{{- $name := .Chart.Name }}
{{- $globalAnnotationsCheck := include "global.annotations.check" . }}
{{- range $index, $val := .Values.items }}
{{- if and $val.imagePullSecrets $val.imagePullSecrets.enabled $val.imagePullSecrets.data }}
apiVersion: v1
kind: Secret
metadata:
  labels:
    app: {{ $name }}
    tier: {{ $val.name | default (print $name) }}-dockerconfig
  name: {{ $val.imagePullSecrets.name | default (print $name "-dockerconfig") }}
type: {{ $val.imagePullSecrets.data.type | default (print "kubernetes.io/dockerconfigjson") }}
data:
  {{- if and $val.imagePullSecrets.data.registry $val.imagePullSecrets.data.login $val.imagePullSecrets.data.password }}
  .dockerconfigjson: {{ printf "{\"auths\":{\"%v\":{\"auth\":\"%v\",\"password\":\"%v\",\"username\":\"%v\"}}}"
$val.imagePullSecrets.data.registry (printf "%v:%v"
$val.imagePullSecrets.data.login
$val.imagePullSecrets.data.password | b64enc)
$val.imagePullSecrets.data.password
$val.imagePullSecrets.data.login | b64enc }}
  {{- else }}
  {{- with $val.imagePullSecrets.data }}{{- toYaml . | nindent 2 }}{{- end }}
  {{- end }}
{{ print "---" }}
{{- end }}{{- end }}
{{- range $index, $val := .Values.items }}
{{- if and $val.tls $val.tls.enabled $val.tls.data }}
apiVersion: v1
kind: Secret
metadata:
{{- if or $val.tls.annotations $globalAnnotationsCheck }}
  annotations:
{{- if and $val.annotations (not (eq $val.type "global")) }}
{{- toYaml $val.tls.annotations | nindent 4 }}
{{- end }}
{{- if $globalAnnotationsCheck }}
{{- range $Index, $Val := $.Values.items }}
{{- if eq $Val.type "global" }}
{{- toYaml $Val.annotations | nindent 4 }}
{{- end }}{{- end }}{{- end }}
{{- end }}
  labels:
    app: {{ $name }}
    tier: {{ $val.name | default (print $name) }}-secret-tls
  name: {{ $val.tls.name | default (print $name "-secret-tls") }}
type: {{ $val.tls.data.type | default (print "kubernetes.io/tls") }}
data:
  {{- if and $val.tls.data.key $val.tls.data.crt }}
  tls.key: {{ printf "%v" $val.tls.data.key | b64enc }}
  tls.crt: {{ printf "%v" $val.tls.data.crt | b64enc }}
  {{- else }}
  {{- with $val.tls.data }}{{- toYaml . | nindent 2 }}{{- end }}
  {{- end }}
{{ print "---" }}
{{- end }}{{- end }}
{{- range $index, $val := .Values.items }}
{{- if not (empty $val.containers) }}
{{- range $Index, $Containers := $val.containers }}
{{- if and $Containers.secret $Containers.secret.enabled $Containers.secret.data }}
apiVersion: v1
kind: Secret
metadata:
{{- if or $val.annotations $globalAnnotationsCheck }}
  annotations:
{{- if and $val.annotations (not (eq $val.type "global")) }}
{{- toYaml $val.annotations | nindent 4 }}
{{- end }}
{{- if $globalAnnotationsCheck }}
{{- range $Index, $Val := $.Values.items }}
{{- if eq $Val.type "global" }}
{{- toYaml $Val.annotations | nindent 4 }}
{{- end }}{{- end }}{{- end }}
{{- end }}
  labels:
    app: {{ $name }}
    tier: {{ $val.name | default (print $name) }}-secret
  name: {{ $Containers.secret.name | default (print $name "-secret") }}
type: Opaque
data:
  {{- range $key, $value := $Containers.secret.data }}{{- if $value }}
  {{- printf "%v: %v" $key (printf "%v" $value | b64enc) | nindent 2 }}
  {{- end }}{{- end }}
{{ print "---" }}
{{- end }}{{- end }}{{- end }}{{- end }}
